const arr = [1, 2, 3, 4];

var exchange = function (nums) {
  let l = 0,
    r = nums.length - 1;
  while (l < r) {
    while (nums[l] % 2 === 1 && l < r) {
      l++;
    }
    while (nums[r] % 2 === 0 && l < r) {
      r--;
    }
    [nums[l], nums[r]] = [nums[r], nums[l]];
    l++;
    r--;
  }
  return nums;
};

exchange(arr);
